/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.dialog.xpath.view;

import java.net.URL;
import java.util.HashMap;
import offset.nodes.Constants;
import offset.nodes.client.model.AppletUtil;
import offset.nodes.client.model.Server;
import offset.nodes.client.model.ServerModel;
import offset.nodes.client.view.SimpleDialog;
import offset.nodes.client.dialog.xpath.model.XPathModel;
import offset.nodes.client.model.DialogContext;
import offset.nodes.client.view.BaseApplet;
import offset.nodes.client.xpath.view.XPathPanel;

/**
 *
 * @author Walter Lütgenau
 */
public class EditXPathQueryApplet extends BaseApplet {

    URL service = null;
    URL repository = null;
    String key = null;
    String path = null;

    /** Creates a new instance of NewNodeApplet */
    public EditXPathQueryApplet() {
    }

    public String getPath() {
        return path;
    }

    public String getKey() {
        return key;
    }

    public URL getService() {
        return service;
    }

    @Override
    public void initUI() {
        try {
            service = new URL(getParameter("service"));
            repository = new URL(getParameter("base"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        path = getParameter("path");
        key = getParameter("key");

        DialogContext.setInstance(DialogContext.EDIT);
    }

    String getParent(String path) {
        int index = path.lastIndexOf("/");
        if (index < 0)
            return path;
        return path.substring(0, index);
    }

    @Override
    public void startUI() {
        // show dialog
        try {
            Server server = new ServerModel(getService());

            // create panel
            XPathPanel panel = new XPathPanel(server);
            XPathModel model = new XPathModel(server);
            
            // initialize panel
            if (key != null)
                panel.setXPathQuery(model.getNodeTypes(), model.getTemporaryXPathQuery(getKey()));
            else
                panel.setXPathQuery(model.getNodeTypes(), model.getXPathQuery(getPath()));

            // wrap panel in dialog
            SimpleDialog dialog = new SimpleDialog(new javax.swing.JFrame(), true, panel);
            dialog.setLocation(100, 100);
            dialog.setSize(555, 720);
            dialog.setVisible(true);
            int result = dialog.getResult();
            dialog.dispose();

            // store result
            URL page = null;

            if (result == SimpleDialog.OK) {
                try {
                    // send modified query to server
                    if (getKey() == null) // no temporary edit
                        model.setXPathQuery(getPath(), panel.getXPathQuery());
                } catch (Exception e) {
                    e.printStackTrace();
                }

                String query = panel.getXPathQuery();

                HashMap<String,String[]> parameters = new HashMap<String,String[]>();
                parameters.put(Constants.PAR_CONTENT_TYPE, new String[] { Constants.PARVALUE_SEARCH });
                parameters.put(Constants.PAR_QUERY, new String[] { query });
                parameters.put(Constants.PAR_QUERY_TYPE, new String[] { Constants.PARVALUE_XPATH });

                page = new URL(repository.toString() + getPath() + AppletUtil.createQuery(parameters, "UTF-8"));
            }
            else
                page = new URL(repository.toString() + getPath());


            // update page
            getAppletContext().showDocument(page);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
